<!doctype html>
<html class="no-js">

<head>
    <title>幼儿园</title>
    <#include "/boya/template/meta.html" />
</head>

<body>
    <#import "/boya/template/marc.html" as mc />
    <@mc.content>
        <div id="app" class="page">
            <div class="title">证书管理 / <span v-if="info.id == ''">新增记录</span><span v-else>编辑记录</span> /</div>
            <el-form ref="form" label-width="80px">
                <el-form-item label="标题">
                    <el-input v-model="info.title" placeholder="请输入标题"></el-input>
                </el-form-item>
                <el-form-item label="附件">
                    <el-upload action="/boya/api/uploadFile" :on-success="handleSuccess" :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove" :limit="10" :on-exceed="handleExceed" :file-list="fileList">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submit">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
    </@mc.content>
    <script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data() {
            return {
                info: {
                    id: '',
                    title: '',
                    images: ''
                },
                fileList: [],
            }
        },
        mounted: function() {
            var self = this;
            var infoJsonStr = '${info!}';
            if (infoJsonStr != '') {
                var obj = JSON.parse(infoJsonStr);
                console.log(obj);
                self.info.id = obj.id;
                self.info.title = obj.title;
                self.info.images = obj.images;
            }
            if (self.info.images && self.info.images != '') {
                var list = self.info.images.split(";");
                list.pop();
                for (var i = 0; i < list.length; i++) {
                    var map = {
                        name: list[i].split("=")[1],
                        url: list[i]
                    }
                    self.fileList.push(map);
                }
            }
        },
        methods: {
            submit() {
                var self = this;
                if (self.info.title == '') {
                    msg("请输入证书标题");
                    return;
                }
                if (self.fileList.length == 0) {
                    msg("请上传证书");
                    return;
                }
                var files = '';
                for (var i = 0; i < self.fileList.length; i++) {
                    files += self.fileList[i].url + ";";
                }
                var map = {
                    title: self.info.title,
                    images: files
                }
                if (self.info.id != '') {
                    map['id'] = self.info.id;
                }
                var param = Qs.stringify(map);
                axios({
                        url: '/boya/submitCert',
                        method: 'post',
                        data: param,
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        }
                    })
                    .then(function(response) {
                        if (response.data.status == 200) {
                            var referer = '${referer!}';
                            if (referer) {
                                window.location.href = referer;
                            } else {
                                window.location.href = "/boya/certList";
                            }
                        } else {
                            msg("保存失败：" + response.data.msg);
                            console.log(response.data);
                        }
                    })
                    .catch(function(error) {
                        msg("保存失败");
                        console.log(error);
                    });
            },
            delFile(index) {
                var self = this;
                confirm("确认删除？", function() {
                    self.fileList.splice(index, 1);
                })
            },
            dateFtt(fmt, date) {
                var o = {
                    "M+": date.getMonth() + 1, //月份
                    "d+": date.getDate(), //日
                    "h+": date.getHours(), //小时
                    "m+": date.getMinutes(), //分
                    "s+": date.getSeconds(), //秒
                    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                    "S": date.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt))
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt))
                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            },
            handleSuccess(response, file, fileList) {
                var self = this;
                // self.fileList = new Array;
                self.fileList.push({
                    name: response.info.split("=")[1],
                    url: response.info
                });
            },
            handlePreview(file) {
                window.location.href = file.url;
            },
            handleRemove(file, fileList) {
                this.fileList = fileList;
            },
            handleExceed(files, fileList) {
                this.$message.warning('只能上传 10 个文件');
            },
            beforeRemove(file, fileList) {
                return this.$confirm('确定移除 ' + file.name + '？');
            }
        }
    })
    </script>
    <style type="text/css">
    .page {
        padding: 15px;
    }

    .title {
        margin-bottom: 3rem;
        font-size: 1.2em;
    }

    .layui-form-label {
        width: 100px;
    }

    .file-item {
        padding: 1rem;
    }

    .file-item .file-item-del {
        margin-left: 1rem;
        color: red;
        cursor: pointer;
    }

    .submit-wrap {
        padding-left: 110px;
    }

    .el-upload__input {
        display: none !important
    }

    .hidden {
        display: none;
    }
    </style>
</body>

</html>